package com.zh.user.controller;

import com.zh.user.entity.SysRole;
import com.zh.user.entity.SysUser;
import com.zh.user.service.SysRoleService;
import com.zh.user.service.SysUserService;
import com.zh.utils.JwtUtil;
import com.zh.utils.PasswordGenerateUtil;
import com.zh.utils.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @ClassName LoginController
 * @date: 2021.04.12 15:38
 * @Author: zhanghang
 */
@RestController
public class LoginController {
    @Resource
    private SysUserService userService;

    @Autowired
    private SysRoleService sysRoleService;
    @GetMapping({"/login", "/"})
    public String showIndex() {
        return "login";
    }
    @PostMapping(value = {"/tologin"})
    public String tologin(@RequestBody SysUser account) {
        System.out.println("当前登录账号：" + account);
//        //1.获取当前用户
//        Subject subject = SecurityUtils.getSubject();
//        //2.封装用户登录数据
//        UsernamePasswordToken token = new UsernamePasswordToken(account.getUsername(), account.getPassword());
        // 生成token
        SysUser user = userService.getUserByUsername(account.getUsername());
        if (null == user){
            return "账号不正确";
        }
        String password = PasswordGenerateUtil.getPassword(account.getUsername(), account.getPassword());
        if (!user.getPassword().equals(password)){
            return "密码不正确";
        }
        try {
            // 获取token
            String token = "";
            // 判断当前token 是否已经存在了
            if (ShiroUtil.isExistTokenByUsername(account.getUsername())){
                token = ShiroUtil.getTokenByUsername(account.getUsername());
                ShiroUtil.refreshToken(token,account.getUsername());
            }else {
                token = JwtUtil.sign(account.getUsername());
                // 将token 放入到redis 中
                ShiroUtil.refreshToken(token,account.getUsername());
            }
//            subject.login(token);//执行登录,没有异常，说明ok
            return "token:"+token;
        } catch (UnknownAccountException ex) {//用户名不存在
//            model.addAttribute("msg", "用户名错误");
            return "login";
        } catch (IncorrectCredentialsException ex) {
//            model.addAttribute("msg", "密码错误");
            return "login";
        }
    }

    @RequestMapping("/main")
    public String main() {
        return "main";

    }

    @RequestMapping("/sys/showview")
    public String showView() {
        //1.获取当前用户
        Subject subject = SecurityUtils.getSubject();
        SysUser user = (SysUser) subject.getPrincipal();
        //根据角色进入不同的界面
        List<String> roleList = sysRoleService.getRoleByUsername(user.getUsername());
        String roleCode = roleList.get(0);
        if ("admin".equals(roleCode)) {
            return "view/admin";
        } else if ("user".equals(roleCode)) {
            return "view/user";
        } else if ("company".equals(roleCode)) {
            return "view/company";
        } else {
            return "";
        }
    }
    /**
     * 无权限错误界面
     *
     * @return
     */
    @RequestMapping("/noauth")
    public String noauth() {
        return "/error/401";
    }
    @RequestMapping("/sys/logout")
    public String logout() {
        //1.获取当前用户
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login";
    }
}
